using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gear : MonoBehaviour
{
    public ItemData.ItemType type;
    public float rate;

    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Init(ItemData data)
    {
        name = "Gear" + data.itemID;
        transform.parent = GameManager.instance.player.transform;
        transform.localPosition = Vector3.zero;

        type = data.itemType;
        rate = data.damages[0];
        ApplyGear();
    }

    public void LevelUp(float rate)
    {
        this.rate = rate;
        ApplyGear();
    }

    private void RateUp()
    {
        MeeleWeapon[] meeleWeapons = transform.parent.GetComponentsInChildren<MeeleWeapon>();
        foreach (MeeleWeapon meeleWeapon in meeleWeapons)
        {
            switch (meeleWeapon.id)
            {
                case 0:
                    float speed =150 * Character.WeapenSpeed;
                    meeleWeapon.speed = speed + (speed * rate);
                    break;
            }
        }

        RangeWeapon[] rangeWeapons = transform.parent.GetComponentsInChildren<RangeWeapon>();
        foreach (RangeWeapon rangeWeapon in rangeWeapons)
        {
            switch (rangeWeapon.id)
            {
                case 1:
                    float speed = 0.5f * Character.WeapenRate;
                    rangeWeapon.speed = speed * (1f - rate);
                    break;
            }
        }
    }

    private void SpeedUp()
    {
        float speed = 5 * Character.Speed;
        GameManager.instance.player.moveSpeed = speed + speed * rate;
    }

    private void ApplyGear()
    {
        switch (type)
        {
            case ItemData.ItemType.Glove:
                RateUp();
                break;
            case ItemData.ItemType.Shoe:
                SpeedUp();
                break;
        }
    }
}
